package game;

import java.util.ArrayList;
import java.util.List;

import dataAccess.*;
import fields.Ownable;
import fields.Territory;

public class GameOptions {

	MySQLPawnedFieldDAO pawnedDB = new MySQLPawnedFieldDAO();
	MySQLHouseDAO houseDB = new MySQLHouseDAO();
	MySQLPlayerDAO playerDB = new MySQLPlayerDAO();
	MySQLFieldDAO fieldDB = new MySQLFieldDAO();
	MySQLLuckyCardDAO cardDB = new MySQLLuckyCardDAO();
	BoundaryGUI out = new BoundaryGUI();

	public void NewGame(ArrayList<Player> players) throws DALException {

		GameDB.EndGame();
		GameDB.NewGame();

		int numberOfPlayers = out.getNumberOfPlayers();
		players = out.addPlayers(players, numberOfPlayers);

		for (int i = 0; i < numberOfPlayers; i++) {
			players.get(i).setPlayerID(i + 1);
			PlayerDTO temp = new PlayerDTO(i + 1, players.get(i).getName(),
					30000, 0, 0, 1);
			playerDB.createPlayer(temp);
		}
	}

	public void LoadField(Gameboard board) throws DALException {
		for (int i = 1; i <= 40; i++) {
			if (board.getField(i) instanceof Ownable) {
				if (board.getField(i) instanceof Territory) {
					HouseDTO temp = houseDB.getHouse(i);
					if (temp.getHotel())
						((Territory) board.getField(i)).setHouses(5);
					else
						((Territory) board.getField(i)).setHouses(temp
								.getHouses());
					out.setHouses(i, temp.getHouses());
				}
				PawnedFieldDTO temp = pawnedDB.getPawnedField(i);
				if (temp.isPawned())
					((Ownable) board.getField(i)).setPawned();
			}
		}
	}

	public ArrayList<LuckyCard> LoadLuckyCards() throws DALException {
		List<LuckyCardDTO> temp = cardDB.getLuckyCardList();
		ArrayList<LuckyCard> tempcard = new ArrayList<LuckyCard>();
		for (int i = 0; i < temp.size(); i++) {
			tempcard.add(new LuckyCard(temp.get(i).getDescription(), temp
					.get(i).getCommand(), temp.get(i).getBonus(), temp.get(i)
					.getJailcard()));
		}
		return tempcard;
	}

	public ArrayList<Player> LoadPlayer(Gameboard board) throws DALException {
		// Spillerne loades.
		ArrayList<Player> players = new ArrayList<Player>();
		Player temp;

		List<FieldDTO> tempDTO;
		Ownable tempField;
		int tempAccount;

		for (int i = 1; i <= playerDB.getPlayerList().size(); i++) {

			temp = new Player(playerDB.getPlayer(i).getPlayer_ID(), playerDB
					.getPlayer(i).getName(),
					playerDB.getPlayer(i).getAccount(), 0, playerDB
							.getPlayer(i).getField(), playerDB.getPlayer(i)
							.getTurnsInJail(), (playerDB.getPlayer(i)
							.getField() == 31), false, playerDB.getPlayer(i)
							.getJailcard(), new PropertyManager(), false);

			tempAccount = playerDB.getPlayer(i).getAccount();
			players.add(i - 1, temp);
			out.LoadPlayer(temp, i - 1);

			// De ejede felter bliver lagt ind i spillerens propertymanager.
			tempDTO = fieldDB
					.getFieldList(playerDB.getPlayer(i).getPlayer_ID());

			for (int j = 0; j < fieldDB.getFieldList(
					playerDB.getPlayer(i).getPlayer_ID()).size(); j++) {

				tempField = (Ownable) board.getField(tempDTO.get(j)
						.getFieldID());

				tempField.buy(temp);
				out.setOwner(tempField.getNumber(), playerDB.getPlayer(i)
						.getName());
			}

			temp.setAccount(tempAccount);

		}

		return players;
	}

	public void saveGame(ArrayList<Player> players,
			ArrayList<Ownable> ownedFields, ArrayList<Ownable> pawnedFields,
			ArrayList<Territory> houseFields, List<LuckyCard> luckycards)
			throws DALException {
		// Spillerne gemmes.
		for (int i = 0; i < players.size(); i++) {
			Player p = players.get(i);
			PlayerDTO temp = new PlayerDTO(p.getPlayerID(), p.getName(),
					p.getAccount(), p.outOfJailCards(), p.getField(),
					p.getTurnsInJail());
			playerDB.updatePlayer(temp);
		}

		// De købte felter gemmes.
		for (int i = 0; i < ownedFields.size(); i++) {
			Ownable field = ownedFields.get(i);
			FieldDTO temp;
			if (field.getOwner()!=null) {	
				temp = new FieldDTO(field.getNumber(), field.getOwner()
					.getPlayerID(), field.getName(), "Ownable");
				fieldDB.updateField(temp);
			}
		}

		// Pantsatte felter gemmes i DB.
		for (int i = 0; i < pawnedFields.size(); i++) {
			Ownable field = pawnedFields.get(i);
			if (field.getOwner()!=null) {
				PawnedFieldDTO temp = new PawnedFieldDTO(field.getNumber(),
					field.getNumber(), field.getPrice() / 2, field.isPawned());
				pawnedDB.updatePawnedField(temp);
			}
		}

		// Grunde i huse gemmes i DB.
		for (int i = 0; i < houseFields.size(); i++) {
			Territory field = houseFields.get(i);
			HouseDTO temp;
			if (field.getOwner()!=null) {
				if (field.getHouses() == 5)
					temp = new HouseDTO(field.getNumber(), field.getNumber(), 0,
						true);
				else
					temp = new HouseDTO(field.getNumber(), field.getNumber(),
						field.getHouses(), false);
				houseDB.updateHouse(temp);
			}
		}

		List<LuckyCardDTO> tempCard = new ArrayList<LuckyCardDTO>();
		for (int i = 0; i < luckycards.size(); i++) {
			LuckyCard card = luckycards.get(i);
			LuckyCardDTO temp;
			temp = new LuckyCardDTO(i + 1, card.getSentence(),
					card.getCommand(), card.getAmount(), card.isOutOfJailCard());
			tempCard.add(temp);
		}

		cardDB.updateLuckyCardList(tempCard);
		
		for (int i = 0; i < playerDB.getJailCards(); i++) {
			cardDB.deleteLuckyCard(cardDB.getLuckyCardList().size() - i);
		}


		// ArrayListene nulstilles.
		ownedFields = new ArrayList<Ownable>();
		pawnedFields = new ArrayList<Ownable>();
		houseFields = new ArrayList<Territory>();
		luckycards = new ArrayList<LuckyCard>();
	}

	public void saveJailCards(int JailcardsUsed) throws DALException {
		for (int i = 0; i < JailcardsUsed; i++) {
			LuckyCardDTO tempLucky = new LuckyCardDTO(cardDB.getLuckyCardList()
					.size() + 1,
					" ", " ",
					0, false);
			cardDB.createLuckyCard(tempLucky);
		}
	}
}
